package com.xyp.mesadmin.log.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xyp.mesadmin.log.mapper.LogMapper;
import com.xyp.mesadmin.log.param.LogPageParam;
import com.xyp.mesadmin.log.service.LogService;
import com.xyp.mescommon.common.PageParam;
import com.xyp.mescommon.entity.log.Log;
import com.xyp.mescommon.entity.md.MdUnitMeasure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * --- 加油，努力 ---
 *
 * @Author XYP
 * @Date 2024/9/29
 * @Solgne 从想做到能做
 * @Desc
 */

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    private LogMapper logMapper;

    @Override
    public void addLog(Log log) {
        logMapper.insertLog(log);
    }

    @Override
    public PageInfo<Log> findAll(LogPageParam logPageParam) {
        PageHelper.startPage(logPageParam.getPageNum(), logPageParam.getPageSize());
        List<Log> logs = logMapper.selectAll(logPageParam.getName(),logPageParam.getStartTime(),logPageParam.getEndTime());
        PageInfo<Log> logPageInfo = new PageInfo<>(logs);
        return logPageInfo;
    }

    @Override
    public void deleteById(Integer id) {
        logMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(List<Integer> ids) {
        logMapper.deleteBatch(ids);
    }

    @Override
    public void exportExcel(HttpServletResponse response) {
        List<Log> logs = logMapper.selectAll(null,null,null);
        if (logs == null){
            throw new RuntimeException("数据不存在");
        }
        try {
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            String fileName = "日志数据";
            fileName = URLEncoder.encode(fileName, "utf-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            EasyExcel.write(response.getOutputStream(),Log.class).sheet("模板").doWrite(logs);
        } catch (IOException e) {
        }
    }
}
